
package pos.util;

import pos.exception.POSException;
import pos.interfaces.Messages;
import java.util.regex.*;

/**
 * This class is used to do the validation for the input text.
 * @author srikanth ramu
 */
public class Validate implements Messages{
   /**
    * Check if the store card number is valid
    * @param inputText
    * @return
    * @throws POSException
    */
   public String validStoreCardNumber (String inputText) throws POSException{
        
        Pattern storeCardNum = Pattern.compile("[\\d]{1,}");
        Matcher matchStoreCardNum;

        validTextInput(inputText);
        
        matchStoreCardNum = storeCardNum.matcher(inputText.trim());
        
        if(!matchStoreCardNum.matches()){
            throw new POSException(Messages.validStoreCard);
        }
        return inputText;
    }
    /**
     * Check if the payment card number is valid
     * @param inputText
     * @return
     * @throws POSException
     */
    public String validPaymentCardNumber (String inputText) throws POSException{

        Pattern paymentCardNum = Pattern.compile("[\\d]{16,19}");
        Matcher matchPaymentCardNum;

        validTextInput(inputText);

        matchPaymentCardNum = paymentCardNum.matcher(inputText.trim());

        if(!matchPaymentCardNum.matches()){
            throw new POSException(Messages.validPaymentCard);
        }
        return inputText;
    }
   /**
    * Check if the month is valid
    * @param inputText
    * @return
    * @throws POSException
    */
    public String validMonth (String inputText) throws POSException{

        Pattern month = Pattern.compile("[\\d]{2}");
        Matcher matchMonth;

        validTextInput(inputText);

        matchMonth = month.matcher(inputText.trim());

        if(!matchMonth.matches()){
            throw new POSException(Messages.validMonth);
        }
        return inputText;
    }
   /**
    * Check if the year is valid
    * @param inputText
    * @return
    * @throws POSException
    */
    public String validYear (String inputText) throws POSException{

        Pattern year = Pattern.compile("[\\d]{4}");
        Matcher matchYear;

        validTextInput(inputText);

        matchYear = year.matcher(inputText.trim());

        if(!matchYear.matches()){
            throw new POSException(Messages.validYear);
        }
        return inputText;
    }
   /**
    * Check if the payment card security number is valid
    * @param inputText
    * @return
    * @throws POSException
    */
    public String validCardSecurityNumber (String inputText) throws POSException{

        Pattern CardSecurityNum = Pattern.compile("[\\d]{3}");
        Matcher matchCardSecurityNum;

        validTextInput(inputText);

        matchCardSecurityNum = CardSecurityNum.matcher(inputText.trim());

        if(!matchCardSecurityNum.matches()){
            throw new POSException(Messages.validCardSecurityNumber);
        }
        return inputText;
    }
    /**
     * Check if the address is valid
     * @param inputText
     * @return
     * @throws POSException
     */
    public String validAddress (String inputText)throws POSException {
        int maxAddressLength = 45;
        validTextInput (inputText);
        if(inputText.length() > maxAddressLength ){
            throw new POSException(Messages.validAddress);
        }
        return inputText;
    }
    /**
     * Check if the unit number is valid
     * @param inputText
     * @return
     * @throws POSException
     */
    public String validUnitNum (String inputText)throws POSException {
        int maxUnitLength = 45;
        validTextInput (inputText);
        if(inputText.length() > maxUnitLength ){
            throw new POSException(Messages.validUnit);
        }
        return inputText;
    }
    /**
     * Check if the phone number is valid
     * @param inputText
     * @return
     * @throws POSException
     */
    public String validPhoneNumber (String inputText) throws POSException{
        
        Pattern phoneNum = Pattern.compile("[\\d]{10}");
        Matcher matchPhoneNum;

        validTextInput(inputText);
        
        matchPhoneNum = phoneNum.matcher(inputText.trim());
        
        if(!matchPhoneNum.matches()){
            throw new POSException(Messages.validPhoneNum);
        }
        return inputText;
    }
    /**
     * Check id the email address is valid
     * @param inputText
     * @return
     * @throws POSException
     */
    public String validEmail (String inputText) throws POSException{
        int maxEmailLength =100;
        Pattern email = Pattern.compile("[\\w]*[\\w|\\.|\\_]*\\w@(\\w+\\.)+[a-z]{2,3}");
        Matcher matchEmail;

        validTextInput(inputText);

        matchEmail = email.matcher(inputText.trim());

        if(!matchEmail.matches()){
            throw new POSException(Messages.validEmail);
        }
        if(inputText.length() > maxEmailLength ){
            throw new POSException(Messages.validEmail);
        }
        return inputText;
    }
   /**
    * Check if the postal code is valid
    * @param inputText
    * @return
    * @throws POSException
    */
    public String validPostalCode (String inputText) throws POSException{

        Pattern postalCode = Pattern.compile("[A-Za-z][\\d][A-Za-z][\\d][A-Za-z][\\d]");
        Matcher matchPostalCode;

        validTextInput(inputText);

        matchPostalCode = postalCode.matcher(inputText.trim());

        if(!matchPostalCode.matches()){
            throw new POSException(Messages.validPostalCode);
        }
        return inputText;
    }
   /**
    * Check if the input text is not empty or null
    * @param inputText
    * @return
    * @throws POSException
    */
    public String validTextInput(String inputText) throws POSException{
        if(inputText == null){
            throw new POSException(Messages.NULLVALE);
        }
        else {
            inputText = inputText.trim();
            if(inputText.isEmpty()){
                throw new POSException(Messages.NULLVALE);
            }
        }
        return inputText;
    }
}
